﻿using GalaSoft.MvvmLight.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class ResignService : IEntityService<ThoiViec>
    {
        private NhanVien _staff;

        public ResignService()
        {
            if (SimpleIoc.Default.IsRegistered<NhanVien>()) _staff = SimpleIoc.Default.GetInstance<NhanVien>();
            else
            {
                throw new Exception("Staff not found");
            }
        }

        public List<ThoiViec> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NhanViens.Include("ThoiViecs")
                    .Where(x => x.MaNhanVien == _staff.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    return query.ThoiViecs.ToList();
                }
                else
                {
                    throw new Exception("Null Staff");
                }
            }
        }

        public bool Add(ThoiViec entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entity.MaNV = _staff.MaNhanVien;
                entities.ThoiViecs.Add(entity);
                entities.SaveChanges();
                return true;
            }
        }

        public bool Update(ThoiViec entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entity.MaNV = _staff.MaNhanVien;
                var query = entities.ThoiViecs.Where(x => x.MaTV == entity.MaTV).FirstOrDefault();
                if (query != null)
                {
                    query.MaNV = entity.MaNV;
                    query.LyDoThoiViec = entity.LyDoThoiViec;
                    query.NgayThoiViec = entity.NgayThoiViec;
                    query.TroCap = entity.TroCap;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(ThoiViec entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.ThoiViecs.Where(x => x.MaTV == entity.MaTV).FirstOrDefault();
                if (query != null)
                {
                    entities.ThoiViecs.Remove(query);
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool DeleteList(List<ThoiViec> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in list)
                {
                    var query = entities.ThoiViecs.Where(x => x.MaTV == item.MaTV).FirstOrDefault();
                    if (query != null)
                    {
                        entities.ThoiViecs.Remove(query);
                    }
                }
                entities.SaveChanges();
                return true;
            }
        }

        public List<ThoiViec> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NhanViens.Include("ThoiViecs")
                    .Where(x => x.MaNhanVien == _staff.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    return query.ThoiViecs.OrderBy(p => p.MaTV).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    throw new Exception("Null Staff");
                }
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            int count = this.GetAll().Count;
            int pages = count / pageSize;
            if (count > pages * pageSize) return pages + 1;
            return pages;
        }
    }
}
